<template>
  <el-select :value="value+''"
             v-loading="loading"
             @input="$emit('input', $event)"
             :style="{width: width+'%'}"
             filterable
             popper-class="client_psa_psau_popper_box"
             :placeholder="placeholder"
             clearable
             :disabled="disabled"
             @change="onChangeClient">
    <el-option :label="data.tenantName"
               v-for="data in dataList"
               :key="data.id"
               :value="data.id">
      <div class="client_psa_psau_box">
        <div class="client_psa_psau_header">
          <span style=" font-size: 14px"
                v-if="isShowCreate?isShowCreate:false">{{ data.tenantName }}</span>
          <span style=" color: #8492a6; font-size: 13px"
                v-if="isShowCreate?isShowCreate:false">{{ data.createDate|ChicagoFormat }}</span>
          <span style=" font-size: 14px"
                v-else>{{ data.tenantName }}</span>
        </div>
        <div class="client_psa_psau_content">
          <div class="client_psa_psau">
            <div><span>{{$t('custom.component.selectClientId.psa')}}:</span><span>&nbsp;{{data.psa}}</span></div>
            <div><span>{{$t('custom.component.selectClientId.psau')}}:</span><span>&nbsp;{{data.psau}}</span></div>
          </div>
          <div class="client_psa_sync"><span>{{$t('custom.component.selectClientId.psaSync')}}:</span> <span>&nbsp;{{momentFormat(data.psaSyncDate) }}</span></div>
        </div>
      </div>
    </el-option>

  </el-select>
</template>
<script>
import moment from 'moment'
export default {
  name: 'SelectClientId',
  data () {
    return {
      dataList: [],
      placeholder: this.$t('custom.component.selectClientId.placeholder'),
      loading: false
    }
  },
  props: {
    value: [Number, String],
    disabled: Boolean,
    width: Number,
    isShowCreate: Boolean,
    isChangeClient: Boolean
  },
  created () {
    if (!this.isChangeClient) {
      this.getClientList()
    }
  },
  methods: {
    momentFormat (data) {
      return data ? moment(data).format('MM/DD/YYYY') : ''
    },
    getClientList (datasetLayerId) {
      this.loading = true
      var _self = this
      var datasetId = 'sys_tenant_list'
      var requestParams = ''
      if (datasetLayerId) {
        requestParams += `datasetLayerId=` + datasetLayerId
      }
      this.$interface.reports.getReportDatasetDataApi(datasetId, requestParams)
        .then(({ data: res }) => {
          if (Array.isArray(res.data)) {
            _self.dataList = []
            _self.dataList = res.data
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    onChangeClient (data) {
      let item = this.dataList.find(e => e.id === data)
      this.$emit('changeClient', item)
    }
  }
}
</script>
<style lang="scss">
.client_psa_psau_popper_box {
  .el-select-dropdown__item {
    padding-top: 10px;
    padding-bottom: 10px;
    height: auto;
    line-height: normal;
  }
  .client_psa_psau_header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .client_psa_psau_content {
    margin-top: 10px;
    span {
      font-size: 12px;
      color: #8492a6;
    }
    .client_psa_psau {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 8px;
    }
    .client_psa_sync {
    }
  }
}
</style>
